var HxBasicGoodsInfoDlg = {
    data: {
        id:"",
        goodsName: "",
        goodsNumber: "",
    }
};
layui.use(['table', 'admin', 'ax', 'laydate', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

	var tableResult

    /**
     * 库存表管理
     */
    var HxStorageStock = {
        tableId: "hxStorageStockTable"
    };

    /**
     * 初始化表格的列
     */
    HxStorageStock.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: '主键'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 80},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},

            {field: 'kcUnit', sort: true, title: '单位', totalRowText: '合计：'},
            {field: 'kcNum', sort: true, title: '数量', totalRow: true},

            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'expirationDate', sort: true, title: '到期日期'},
        ]];
    };

    // 渲染表格
    tableResult = table.render({
        elem: '#' + HxStorageStock.tableId,
        url: Feng.ctxPath + '/hxStorageStock/choicelist?warehouseId='+Feng.getUrlParam("warehouseId"),
        page: true,
        limit: 40,
		limits:[40,60,120,240,360,480,1500,5000,10000,20000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: HxStorageStock.initColumn()
    });

    /**
     * 点击查询按钮
     */
    HxStorageStock.search = function () {
        var queryData = {};
        queryData['goodsName'] = $("#goodsName").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        queryData['condition'] = $("#condition").val();
        table.reload(HxStorageStock.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    //点击选择商品档案
    $('#choiceGoods').click(function () {
        layer.open({
            type: 2,
            title: '商品档案',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/hxBasicGoods/toChoiceGoods',
            end: function () {
                // 商品名称
                $("#goodsName").val(HxBasicGoodsInfoDlg.data.goodsName);
            }
        });
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxStorageStock.search();
    });

    // 重置按钮点击事件
    $('#reset').click(function () {
        $("#goodsName").val("")
        $("#condition").val("")
        $("#positionNumber").val("")
    });

    //回填商品数据
    function confirm(data){
    	parent.HxStorageStockInfoDlg.data.id = data.id;
    	parent.HxStorageStockInfoDlg.data.positionId = data.positionId;
    	parent.HxStorageStockInfoDlg.data.positionNumber = data.positionNumber;
    	parent.HxStorageStockInfoDlg.data.goodsId = data.goodsId;
    	parent.HxStorageStockInfoDlg.data.goodsName = data.goodsName;
    	parent.HxStorageStockInfoDlg.data.goodsNumber = data.goodsNumber;
    	parent.HxStorageStockInfoDlg.data.goodsPcode69 = data.goodsPcode69;
    	parent.HxStorageStockInfoDlg.data.goodsSpecs = data.goodsSpecs;
    	parent.HxStorageStockInfoDlg.data.kcUnit = data.kcUnit;
    	parent.HxStorageStockInfoDlg.data.kcNum = data.kcNum;
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    }

    // 双击行事件
    table.on('rowDouble(' + HxStorageStock.tableId + ')', function (obj) {
        var data = obj.data;
    	confirm(data);
    });

    // 工具条点击事件
    table.on('tool(' + HxStorageStock.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'confirm') {
        	confirm(data);
        }
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});
